package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.FishClassify;
import com.ysd.entity.NewsClassify;
import com.ysd.util.DBManager;

public class FishClassifyDaoImpl {

	public List<FishClassify> getFish(){
		List<FishClassify> FCList = new ArrayList<FishClassify>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from fishclassify");
			while(rs.next()) {
				int classifyId = rs.getInt("classifyId");
				String fishClassifyName = rs.getString("fishClassifyName");
				FishClassify fishList = new FishClassify(classifyId, fishClassifyName);
				FCList.add(fishList);
			}
			return FCList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 分页
	 * @param page
	 * @param size
	 * @return 集合
	 */
	public List<FishClassify> getProCateByPage(int page,int size){
		List<FishClassify> FCList = new ArrayList<FishClassify>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "select * from fishclassify limit "+(page-1)*size+","+size;//limit后不支持数据计算
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int classifyId = rs.getInt("classifyId");
				String fishClassifyName = rs.getString("fishClassifyName");
				FishClassify fishList = new FishClassify(classifyId, fishClassifyName);
				FCList.add(fishList);
			}
			return FCList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 根据产品返回的Id删除产品
	 * @param fishId
	 * @return 数据库受影响行数
	 */
	public int getdelProductCatbyId(int fishId) {
		return DBManager.executeUpdate("delete from fishclassify where classifyId="+fishId);
	}
	
	/**
	 * 根据产品分类名称添加数据
	 * @param f
	 * @return 受影响行数
	 */
	public int addProductClassify(FishClassify f) {
		return DBManager.executeUpdate("insert into fishclassify values(null,'"+f.getFishClassifyName()+"')");
	}
	
	/**
	 * 根据产品名称查询产品
	 * @param name
	 * @return 集合
	 */
	public List<FishClassify> getProCateWhereName(String name){
		List<FishClassify> fishList = new ArrayList<FishClassify>();
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from fishclassify where fishClassifyName='"+name+"'");
			while(rs.next()) {
				int classifyId = rs.getInt("classifyId");
				String fishClassifyName = rs.getString("fishClassifyName");
				FishClassify fish = new FishClassify(classifyId, fishClassifyName);
				fishList.add(fish);
			}
			return fishList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 根据id查询分类
	 * @param fishId
	 * @return int单条数据
	 */
	public FishClassify getfishCateById(int fishId) {
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		FishClassify fc = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from fishclassify where classifyId="+fishId);
			while(rs.next()) {
				int fishCateId = rs.getInt(1);
				String fishCateName = rs.getString(2);
				fc = new FishClassify(fishCateId, fishCateName);
			}
			return fc;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 修改产品分类的所有字段
	 * @param fishc
	 * @return 分类对象
	 */
	public int updateFishCate(FishClassify fishc) {
		String sql = "update fishclassify set fishClassifyName='"+fishc.getFishClassifyName()+"' where classifyId="+fishc.getClassifyId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return 多个id为String
	 */
	public int delFishCateByIds(String ids) {
		String sql = "delete from fishclassify where classifyId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
